package cn.ctvonline.android.kaopu.activity;

import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.ctvonline.android.kaopu.R;
import cn.ctvonline.android.kaopu.base.BaseAppCompatActivity;
import cn.ctvonline.android.kaopu.base.util.WindowUtil;
import cn.ctvonline.android.kaopu.widget.layout.LabelFlowLayout;

/**
 * Created by dujian on 15/12/1.
 */
public class SearchableActivity extends BaseAppCompatActivity {
    @Bind(R.id.search_activity_toolbar)
    Toolbar toolbar;
    @Bind(R.id.search_hot_fl)
    LabelFlowLayout hotSearchFl;
    private SearchView searchView;
    private MenuItem searchItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WindowUtil.translucentStatusBar(this, true);
        WindowUtil.setTranslucentStatus(this, true);
        setContentView(R.layout.activity_searchable);
        initViews();
    }

    private void initViews() {
        ButterKnife.bind(this);
        setSupportActionBar(toolbar);
//        toolbar.setNavigationIcon(getResources().getDrawable(R.mipmap.icon_arrow_left));
//        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                ToastUtil.show(getApplicationContext(), "tool bar navigation clicked");
//                SoftKeyboardUtil.hideSoftKeyboard(getApplicationContext(), v);
//                finish();
//            }
//        });
//        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
//            @Override
//            public boolean onMenuItemClick(MenuItem item) {
//                ToastUtil.show(getApplicationContext(), item.getTitle()+"");
//                return false;
//            }
//        });
        //toolbar 添加监听
        int cCount = hotSearchFl.getChildCount();
        Log.i(TAG, "child count" + cCount);
        for (int i = 0; i < cCount; i++) {
            Log.i(TAG, "child view position=" + i + " set on click listener");
            hotSearchFl.getChildAt(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!searchView.isFocused()) {
                        MenuItemCompat.expandActionView(searchItem);
                    }
                    String hotSearch = ((TextView) v).getText().toString();
                    searchView.setQuery(hotSearch, false);
                }
            });
        }
    }

    private MenuItem settingItem;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_search, menu);
//        menu.findItem(R.id.action_share).setVisible(false);
//        settingItem = menu.findItem(R.id.action_settings);
//        settingItem.setIcon(R.mipmap.icon_more);
//        searchItem = menu.findItem(R.id.action_search);
//        searchView = (SearchView)MenuItemCompat.getActionView(searchItem);
//        if(searchView == null) {
//            searchView = (SearchView) searchItem.getActionView();
//        }
//        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
//        Log.i(TAG, "onCreateOptionsMenu searchView = " + searchView);
//        if(searchView != null){
//            searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
//            searchView.setIconifiedByDefault(true);
//            searchView.setIconified(false);
//            searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
//                @Override
//                public void onFocusChange(View v, boolean hasFocus) {
//                    if (!hasFocus) {
//                        searchItem.collapseActionView();
//                        searchView.setQuery("", false);
//                        settingItem.setVisible(true);
//                    }
//                    if (hasFocus) {
//                        settingItem.setVisible(false);
//                    }
//                }
//            });
//            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
//                @Override
//                public boolean onQueryTextSubmit(String query) {
//                    ToastUtil.show(getApplicationContext(), "提交搜索");
//                    return true;
//                }
//
//                @Override
//                public boolean onQueryTextChange(String newText) {
//                    return false;
//                }
//            });
//            searchView.setOnCloseListener(new SearchView.OnCloseListener() {
//                @Override
//                public boolean onClose() {
//                    ToastUtil.show(getApplicationContext(), "search view close");
//                    return false;
//                }
//            });
//        }
//        MenuItemCompat.expandActionView(searchItem);
//        MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
//            @Override
//            public boolean onMenuItemActionExpand(MenuItem item) {
//                return true;
//            }
//
//            @Override
//            public boolean onMenuItemActionCollapse(MenuItem item) {
//                return true;
//            }
//        });
        return true;
    }

    @Override
    protected String generatePageName() {
        return "pageName SearchableActivity";
    }
}
